package Client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Chatting extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextArea messages = new JTextArea();
	private JTextArea list = new JTextArea();
	private JTextField message = new JTextField();
	private JButton send = new JButton();
	private JButton clrs = new JButton();
	private JScrollPane jSP1 = new JScrollPane();
	private JScrollPane jSP2 = new JScrollPane();
	private JPanel panel1 = new JPanel();
	private JPanel paneBtn = new JPanel();
	private LoginConnection loginConnection;
	public Chatting(String name,LoginConnection loginConnection){
		super(name);
		this.loginConnection = loginConnection;
		this.init();
	}
	private void init(){
		messages.setColumns(30);
		messages.setRows(25);
		messages.setEditable(false);
		messages.setWrapStyleWord(true);
		list.setColumns(10);
		list.setRows(22);
		list.setEditable(false);
		message.setColumns(15);
		send.setText("发送");
		clrs.setText("清屏");
		
		//发送按钮事件
		send.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Chatting.this.sendMessage();
			}
		});
		clrs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Chatting.this.clrScreen(e);
			}
		});
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				try{
					Chatting.this.loginConnection.sendMessage(Chatting.this.loginConnection.getUserName(), "6");
				}catch(Exception e1){
					e1.printStackTrace();
				}
			}
		});
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.message.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){
				if (KeyEvent.VK_ENTER == e.getKeyCode()){
					Chatting.this.sendMessage();
				}
			}
		});
		jSP1.setViewportView(messages);
		jSP2.setViewportView(list);
		panel1.setLayout(new BorderLayout());
		paneBtn.add(message);
		paneBtn.add(send);
		paneBtn.add(clrs);
		panel1.add(jSP1,BorderLayout.NORTH);
		panel1.add(paneBtn,BorderLayout.SOUTH);
		jSP2.setBorder(BorderFactory.createTitledBorder("在线用户列表"));
		panel1.setBorder(BorderFactory.createTitledBorder("聊天信息"));
		this.getContentPane().add(panel1,BorderLayout.WEST);
		this.getContentPane().add(jSP2,BorderLayout.EAST);
		this.pack();
		this.setVisible(true);
		this.setResizable(false);
	}
	private void sendMessage(){
		//获取文本框值
		String strMessage = message.getText();
		//清除文本框
		this.message.setText(null);
		if(strMessage.charAt(0)=='@')
		{
			this.loginConnection.sendMessage(strMessage, "9");
		}
		else {
			this.loginConnection.sendMessage(strMessage, "2");
		}
	}
	private void clrScreen(ActionEvent e){
		messages.setText(null);
	}
	public JTextField getMessage(){
		return message;
	}
	public JTextArea getList(){
		return list;
	}
	public JTextArea getMessages(){
		return messages;
	}
}
